package spring.lottery.server.activityStatus.operate;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import spring.lottery.controller.param.DrawPrizesData;
import spring.lottery.dao.dataObject.ActivityPrizeDO;
import spring.lottery.dao.mapper.ActivityPrizeMapper;
import spring.lottery.server.enums.ActivityStatusEnum;

@Slf4j
@Component
public class PrizeOperate extends AbstractOperate {

    @Autowired
    ActivityPrizeMapper activityPrizeMapper;

    @Override
    public int sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(DrawPrizesData param) {

        ActivityPrizeDO byAPId = activityPrizeMapper.selectByAPId(param.getActivityId(), param.getPrizeId());

        if (param.getActivityId() == null || param.getPrizeId() == null) {
            log.warn("奖品不存在");
            return false;
        }

        if (byAPId == null
                || ActivityStatusEnum.PRIZE_COMPLETED.equals(byAPId.getStatus())) {
            log.warn("奖品已经被抽了");
            return false;
        }

        return true;
    }

    @Override
    public Boolean convert(DrawPrizesData param) {
        if(param == null) {
            log.info("param is null");
            return false;
        }
        try {
            activityPrizeMapper.updateStatus(param, ActivityStatusEnum.PRIZE_COMPLETED);
        }catch (Exception e) {
            log.error("扭转奖品状态出现错误 param: {}, errorInfo:{}", param + " " + ActivityStatusEnum.PRIZE_COMPLETED,
                    e.getMessage());
            return false;
        }


        return true;
    }
}
